package com.android.lazysearch;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class CheckActivity extends Activity 
{
	private ExpandableListAdapter mAdapter;
	private Context mContext;
	private ArrayList<Restaurant> restaurants;
	private GlobalClass appState;
	private TextView tv;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.check);
        mContext = this;
        
        Bundle bundle = this.getIntent().getExtras();        
        restaurants = bundle.getParcelableArrayList("selectedRestaurants");
        
		appState = (GlobalClass) getApplicationContext();
		tv = (TextView) findViewById(R.id.totalAmt);
		tv.setText("$"+String.format("%.2f", appState.getTotal()));        		
		
		mAdapter = new RestaurantAdapter(this, restaurants);
		ExpandableListView menuList = (ExpandableListView) findViewById(R.id.menuList);
		menuList.setAdapter(mAdapter);
	}
	
	//
	public void backToMainPage(View v)
	{
		Intent intent = new Intent(mContext,LazySearch.class);
		Bundle bundle = new Bundle();
		bundle.putParcelableArrayList("selectedRestaurants", restaurants);
		intent.putExtras(bundle);
		setResult(LazySearch.NEW_TAB_REQUEST, intent);
	    finish();
//		mContext.startActivity(intent);
	}
	
//	//Increase the order amount for the proper item in the item list
	public void inc_order(View v)
    {
    	LinearLayout row = (LinearLayout)(v.getParent());
    	ListView lv = (ListView)(findViewById(R.id.menuList));
    	int pos = lv.getPositionForView(row);
    	
    	Item item = (Item)(lv.getItemAtPosition(pos));
    	item.setItemQty(item.getItemQty()+1);
    	((BaseExpandableListAdapter) mAdapter).notifyDataSetInvalidated();
      	Float totalAmt = appState.getTotal();
      	totalAmt = totalAmt + item.getItemPrice();
      	appState.setTotal(totalAmt);
      	tv.setText("$"+String.format("%.2f", appState.getTotal()));
    }
	
	//Decrease the order amount for the proper item in the item list
	public void dec_order(View v)
    {
		//Should get the object associated with the row instead
    	LinearLayout row = (LinearLayout)(v.getParent());
    	ListView lv = (ListView)(findViewById(R.id.menuList));
    	int pos = lv.getPositionForView(row);
    
    	Item item = (Item)(lv.getItemAtPosition(pos));
    	int count = item.getItemQty();
    	if(count>0)
    	{
        	item.setItemQty(item.getItemQty()-1);
        	((BaseExpandableListAdapter) mAdapter).notifyDataSetInvalidated();
          	Float totalAmt = appState.getTotal();
          	totalAmt = totalAmt - item.getItemPrice();
          	appState.setTotal(totalAmt);
          	tv.setText("$"+String.format("%.2f", appState.getTotal()));
    	}
    }
}
